'use strict';
app.controller('rebateGoodsCtrl',['$scope','$stateParams','$modal','utilService','rebateGoodsService','toaster', function($scope,$stateParams,$modal,utilService,rebateGoodsService,toaster){

    $scope.rebateCategory={};

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'rebate/category',
            params:$scope.rebateCategory
        }).then(function(result){
        });

    }

    $scope.query();


    $scope.del = function(id,name){
        var param = {
            id:id,
            name:name
        };

        // query data.
        openModal("你是否要删除【" + name + "】返利类别？");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除返利类别",
                controller:"rebateGoodsDeleteCtrl",
                message:msg,
                data:param
            });
        }
    }


}]);


app.controller('rebateGoodsAddCtrl',['$scope','$state','utilService','rebateGoodsService','toaster', function($scope,$state,utilService,rebateGoodsService,toaster){
    $scope.rebateGoodsService=rebateGoodsService;
    // call server.
    $scope.add=function(){
        rebateGoodsService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.rebateGoods');
                    break;
                case 674:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);


app.controller('rebateGoodsEditCtrl',['$scope','$state','$stateParams','utilService','rebateGoodsService','toaster', function($scope,$state,$stateParams,utilService,rebateGoodsService,toaster){
    $scope.id=$stateParams.id;
    $scope.rebateGoodsService=rebateGoodsService;
    $scope.rebateGoodsService.rebateCategoryEditVO.id=$scope.id;

    $scope.findById=function(){
        rebateGoodsService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    rebateGoodsService.rebateCategoryEditVO=result.data.data;
                    break;
                case 675:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.findById();

    // call server.
    $scope.edit=function(){
        rebateGoodsService.edit($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.rebateGoods');
                    break;
                case 675:
                    toaster.pop('waiting', "提示", result.message);
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

app.controller('rebateGoodsDeleteCtrl', ['$scope', '$modalInstance','$state','rebateGoodsService','toaster','modalData',
    function($scope, $modalInstance,$state,rebateGoodsService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            rebateGoodsService.delete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.query();
                        toaster.pop('success', "提示", "操作成功");;
                        break;
                    case 675:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);